package org.qiuqiu.controller;

import java.io.PrintWriter;
import java.net.UnknownHostException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.qiuqiu.service.UserService;
import org.qiuqiu.service.impl.UserServiceImpl;
import org.qiuqiu.utils.LocalIPUtils;
import org.qiuqiu.utils.MD5Utils;
import org.qiuqiu.utils.SendEmailUtils;
import org.qiuqiu.vo.User;
import org.qiuqiu.vo.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserServiceImpl service;

	// 用户登录
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(Model model, HttpSession session,
			@RequestParam("username") String username,
			@RequestParam("password") String password) throws Exception {
		User user = new User();
		user.setUsername(username);
		user.setPassword(MD5Utils.md5(password));
		
		
		user = service.findBynameAndPassword(user);
		if (user == null) {
			model.addAttribute("msg", "账号或密码错误,请重新登录");
			return "login";
		} /*else if (user.getStatus()==0){
			model.addAttribute("msg", "该账号未激活，请先登录邮箱激活");
			return "login";
		} */else {
			session.setAttribute("user", user);
			// model.addAttribute("user", user);
		}
		return "redirect:/index";
	}

	// 用户注册
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String regester(@RequestParam("userName") String username,
			@RequestParam("password") String password,
			@RequestParam("email") String email, Model m,HttpServletRequest rs) throws Exception {
		User us = new User();
		us.setEmail(email);
		us.setPassword(password);
		us.setUsername(username);
		us.setNickname(username);
		us.setStatus(0);
		us.setUserstatus(0);
		// 加密
		us.setPassword(MD5Utils.md5(us.getPassword()));
		service.insert(us);
		SendEmailUtils.sendEmail(us,rs);
		// 转到登陆页
		return "redirect:/post/all";
	}

	// 激活邮箱登陆
	@RequestMapping(value = "/active")
	public String activateEmail(@RequestParam("checkcode") String checkcode,
			@RequestParam("id") Integer id, Model m, HttpSession session)
			throws Exception {
		User u = service.selectByPrimaryKey(id);
		if (u.getStatus() == 0) {
			if (checkcode.equals(MD5Utils.md5(u.getUsername()))) {
				u.setStatus(1);
				// 更新数据库
				service.updateByPrimaryKeySelective(u);
			} else {
				String msg = "你的用户名无效！请重新注册！";
				m.addAttribute("msg", msg);
			}
		} else {
			String msg = "该用户已注册了！";
			m.addAttribute("msg", msg);
		}
		// 转到激活页面
		return "activate";
	}

	// 个人中心
	@RequestMapping(value = "/person", method = RequestMethod.POST)
	public String personCentered(@RequestParam("nickname") String nickName,
			@RequestParam("password") String password,
			@RequestParam("email") String email, HttpSession session)
			throws Exception {
		User user = (User) session.getAttribute("user");
		user.setStatus(0);
		user.setEmail(email);
		user.setNickname(nickName);
		// user.setPassword(password);
		String s = MD5Utils.md5(password);
		user.setPassword(s);
		service.updateByPrimaryKeySelective(user);
			/*SendEmailUtils.sendEmail(user);*/
			session.removeAttribute("user");
		return "redirect:/login";
	}
	
	// 退出登录
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute("user");
		return "redirect:/index";
	}
	
	// 验证用户名是否存在
	@RequestMapping(value="/exsit",method=RequestMethod.GET)
	public void exsit(@RequestParam("username")String username,HttpServletResponse re) throws Exception{
			User user=new User();
			user.setUsername(username);
			Integer flag=service.findByName(user);
			JSONObject data = new JSONObject();
			data.put("flag", flag);
			re.setContentType("application/json; charset=utf-8");
			PrintWriter  out = re.getWriter();
			out.write(data.toString());
			out.flush();
			out.close();
	}
	
	// 验证昵称是否存在
	@RequestMapping(value="/isNickNameexsit",method=RequestMethod.GET)
	public void isNickNameexsit(@RequestParam("nickname")String nickname,HttpServletResponse re) throws Exception{
		User user=new User();
		user.setNickname(nickname);
		Integer flag=service.findByNickName(user);
		JSONObject data = new JSONObject();
		data.put("flag", flag);
		re.setContentType("application/json; charset=utf-8");
		PrintWriter  out = re.getWriter();
		out.write(data.toString());
		out.flush();
		out.close();
	}
	@RequestMapping(value="/checkpassword",method=RequestMethod.GET)
	public void checkpassword(@RequestParam("oldpassword")String oldpassword,HttpServletResponse re,HttpSession session) throws Exception{
		String flag="no";
		User user=(User) session.getAttribute("user");
		if (user.getPassword().equals(MD5Utils.md5(oldpassword))) {
			flag="ok";
		}
		JSONObject data = new JSONObject();
		data.put("flag", flag);
		re.setContentType("application/json; charset=utf-8");
		PrintWriter  out = re.getWriter();
		out.write(data.toString());
		out.flush();
		out.close();
	}
	
}
